package com.clas.liu.myapplication.model.impl;

import android.util.Log;

import com.clas.liu.myapplication.model.entity.BaseRoot;
import com.clas.liu.myapplication.model.entity.info.InfoRoot;
import com.clas.liu.myapplication.model.event.UserEvent;
import com.clas.liu.myapplication.model.i.IUserModel;
import com.clas.liu.myapplication.network.NetworkInterfaces;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by liu on 2019/5/5.
 */

public class UserModel extends BaseFragmentModel<UserEvent> implements IUserModel {
    public UserModel(OnEventReceiveListener<UserEvent> listener) {
        super(listener);
    }

    @Override
    public void getInfos(int userId, int type,int page_no,int page_size) {
        Callback callback = new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                UserEvent event = new UserEvent();
                event.setWhat(UserEvent.GET_USER_INFO_FAIL);
                postEvent(event);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.isSuccessful()) {
                    Gson gson = new Gson();
                    InfoRoot root = gson.fromJson(response.body().string(), InfoRoot.class);  //json解析
                    UserEvent event = new UserEvent();                        //事件
                    event.setWhat(UserEvent.GET_USER_INFO_OK);                         //事件的设置
                    event.setRoot(root);
                    postEvent(event);
                }else
                    onFailure(call, new IOException(response.body().string()));
            }
        };
        interfaces.getUserInfo(userId,type,page_no,page_size,callback);
    }

    @Override
    public void infoDelete(int id) {
        Callback callback = new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                UserEvent event = new UserEvent();
                event.setWhat(UserEvent.DELETE_INFO_FAIL);
                postEvent(event);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.isSuccessful()) {
                    Gson gson = new Gson();
                    BaseRoot root = gson.fromJson(response.body().string(), InfoRoot.class);  //json解析
                    UserEvent event = new UserEvent();                        //事件
                    event.setWhat(UserEvent.DELETE_INFO_OK);                         //事件的设置
                    event.setBaseRoot(root);
                    postEvent(event);
                }else
                    onFailure(call, new IOException(response.body().string()));
            }
        };
        interfaces.deleteInfo(id,callback);
    }

    @Override
    public void infoSettled(int id) {
        Callback callback = new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                UserEvent event = new UserEvent();
                event.setWhat(UserEvent.SET_INFO_SETTLED_FAIL);
                postEvent(event);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.isSuccessful()) {
                    Gson gson = new Gson();
                    BaseRoot root = gson.fromJson(response.body().string(), InfoRoot.class);  //json解析
                    UserEvent event = new UserEvent();                        //事件
                    event.setWhat(UserEvent.SET_INFO_SETTLED_OK);                         //事件的设置
                    event.setBaseRoot(root);
                    postEvent(event);
                }else
                    onFailure(call, new IOException(response.body().string()));
            }
        };
        interfaces.settledInfo(id,callback);
    }

    @Override
    protected NetworkInterfaces getNetworkInterface() {
        return new NetworkInterfaces();
    }
}
